<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '340px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
      this.setOptions()
    },
    // setOptions({ expectedData } = {}) {
    setOptions() {
      this.chart.setOption({
        // backgroundColor: '#fff',
        // color: ['#3398DB'],
        title: {
          text: '当日就诊量排名前5的科室',
          textStyle: {
            color: '#333',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
          show: false,
          backgroundColor: 'transparent',
          borderColor: 0
        },
        xAxis: {
          show: false
        },
        yAxis: [
          {
            show: true,
            data: ['呼吸科', '消化科', '神经科', '妇科', '骨科'],
            inverse: true,
            axisLine: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#4A5A7F',
              rich: {
                lg: {
                  // backgroundColor: '#339911',
                  borderRadius: 2,
                  padding: 5,
                  align: 'center',
                  width: 30,
                  height: 30
                }
              }
            }
          },
          {
            show: true,
            inverse: true,
            data: [10, 20, 205, 100, 20],
            axisLabel: {
              textStyle: {
                fontSize: 12,
                color: '#212B42'
              }
            },
            axisLine: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            // name: '直接访问',
            type: 'bar',
            yAxisIndex: 1,
            showBackground: true,
            backgroundStyle: {
              color: '#D3F6EA'
            },
            axisPointer: {
              show: false
            },
            barWidth: '10%',
            itemStyle: {
              normal: {
                barBorderRadius: 30,
                backgroundColor: 'transparent',
                color: '#25D096'
              }
            },
            data: [100, 502, 200, 334, 390]
          }

        ]
      })
    }
  }
}
</script>
